import os

import yaml


class YamlHandler:
    def __init__(self, file):
        self.file = file
    # 读取yaml数据 使用yaml的load方法将yaml格式转化为字典格式
    def read_yaml(self, encoding='utf-8'):
        '''读取yaml数据'''
        with open(self.file, encoding='utf-8') as f:
            return yaml.load(f.read(), Loader=yaml.FullLoader)
    # 字典格式内容通过yaml的dump方法转化为yaml格式存回config.yaml文件中
    def write_yaml(self, data, encoding='utf-8'):
        """向yaml文件写入数据"""
        with open(self.file, encoding=encoding, mode='w') as f:
            print('写入成功')
            return yaml.dump(data, stream=f, allow_unicode=True)

path = os.path.abspath(os.path.dirname(__file__))
config_path = path[0:-6].replace('\\', '/') + 'config/config.yaml'  # 无奈之举 强行解决路径问题
yaml_data = YamlHandler(config_path).read_yaml()
# print(yaml_data)
